package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.MaCategoryMapper;
import com.zhentao.mapper.MaProductMapper;
import com.zhentao.model.common.buiness.dto.MaProductDto;
import com.zhentao.model.common.buiness.pojo.Category;
import com.zhentao.model.common.buiness.pojo.Product;
import com.zhentao.model.common.buiness.vo.MaProductAndCategory;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.service.MaCategoryService;
import com.zhentao.service.MaProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 王越潇
 * @version 1.0
 * @Date 2024/9/15 10:17
 * @Type
 */
@Service
public class MaCategoryServiceImpl extends ServiceImpl<MaCategoryMapper, Category> implements MaCategoryService {
    @Resource
    private MaCategoryMapper maCategoryMapper;

    @Override
    public ResponseResult findAll() {
        List<Category> categories = maCategoryMapper.selectList(null);
        return ResponseResult.okResult(categories);
    }

    @Override
    public ResponseResult categoryAdd(Category category) {
        category.setCreatetime(new Date());
        maCategoryMapper.insert(category);
        return ResponseResult.okResult("新增成功");
    }

    @Override
    public ResponseResult categoryUpdate(Category category) {
        category.setUpdatetime(new Date());
        maCategoryMapper.updateById(category);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult categoryDelete(Integer id) {
        maCategoryMapper.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult updateStatus(Integer id) {
        Category category = maCategoryMapper.selectById(id);
        if (category.getStatus() == 1) {
            category.setStatus(2);
            maCategoryMapper.updateById(category);
            return ResponseResult.okResult("禁用成功");
        } else if (category.getStatus() == 2){
            category.setStatus(1);
            maCategoryMapper.updateById(category);
            return ResponseResult.okResult("启用成功");
        }
        return ResponseResult.okResult("修改成功");
    }
}
